package server.command.list;

import java.util.Collection;
import java.util.LinkedList;

import communication.exception.ResultException;
import communication.exception.VerifyParameterException;

import server.command.AnalysisCommand;
import server.command.Command;
import server.core.base.Airplane;
import server.core.base.Airport;
import server.core.base.EntityBase;
import server.core.base.Flight;
import server.core.base.Terrain;
import server.core.base.Ticket;
import server.core.base.User;
import server.core.dao.DaoFactory;
import server.core.manager.Admin;
import server.core.manager.Manager;

public class PrintBase implements Command {

	private AnalysisCommand parsing;
	private static final Class<? extends EntityBase>[] baseList=new Class[]{
		Airplane.class,Airport.class,Flight.class,Terrain.class,Ticket.class,User.class};
	public static Class<? extends EntityBase>[] getBaseList() {
		return baseList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object> execute() throws ResultException {
		Collection<Object> result=new LinkedList<Object>();
		Collection<? extends EntityBase> base = (DaoFactory.getDao(parsing.getTypeOfDao()))
				.getEntityMap((Class<? extends EntityBase>) this.parsing.getArgsMap().get("BASE")).values();
		if(base==null)
			throw new ResultException("Base is empty");
		result.addAll(base);
		result.add("Found: "+result.size());
		return result;
	}

	@Override
	public void verifyParam(AnalysisCommand parsing) throws VerifyParameterException {
		this.parsing=parsing;
		for(String key:AnalysisCommand.paramBase){
			if(parsing.getParamMap().containsKey(key)){
				for(Class c:getBaseList())
					if(parsing.getParamMap().get(key).equalsIgnoreCase(c.getSimpleName()))
						parsing.getArgsMap().put(key, c);
				if(! parsing.getArgsMap().containsKey(key))
					throw new VerifyParameterException("Unknown base`s code in #"+key);
			}
			else
//				this.argsMap.put(key, null);
				throw new VerifyParameterException(
						"Parameter is missing #"+key);
		}
	}

	@Override
	public String help() {
		return " base=";
	}

	@Override
	public Class<? extends Manager> getManager() {
		return Admin.class;
	}

	@Override
	public void exit() {
		// TODO Auto-generated method stub
		
	}

}
